#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include "../include/menu.h"

struct option longOpt[] =
{
    {"help", no_argument, NULL, 'h'},
    {"pipes", no_argument, NULL,'p'},
    {"msg_queue", no_argument, NULL,'q'},
    {"shared_memory", no_argument, NULL,'m'},
    {"sockets", no_argument, NULL,'s'},
    {"fork", no_argument, NULL,'f'},
    {"threads", no_argument, NULL,'t'},
};

void
freeParameters(appInit *p)
{
    return;
}

int
validateParameters(appInit *params)
{

    if (params == NULL)
        return 0;

    if (params->ipcQuantity != 1)
    {
        fprintf(stderr, "An IPC must be chosen.\n");
        return 0;
    }

    if (params->splitQuantity != 1)
    {
        fprintf(stderr, "A split method must be chosen.\n");
        return 0;
    }

    if (params->error)
        return 0;

    return 1;
}

int
getParameters(int argc, char *argv[], appInit *params)
{
    int nextOpt;

    if(params == NULL)
        return -1;

    /*Params gets initialized*/
    params->ipcQuantity = 0;
    params->splitQuantity = 0;
    params->error = 0;

    while( (nextOpt = getopt_long(argc, argv,"hpqmsft", longOpt, NULL)) != -1 )
    {
        switch( nextOpt )
        {
            case 'p':
                params->ipcQuantity++;
                params->ipc = 'p';
                break;

            case 'q':
                params->ipcQuantity++;
                params->ipc = 'q';
                break;

            case 'm':
                params->ipcQuantity++;
                params->ipc = 'm';
                break;

            case 's':
                params->ipcQuantity++;
                params->ipc = 's';
                break;

            case 'f':
                params->splitQuantity++;
                params->split = 'f';
                break;

            case 't':
                params->splitQuantity++;
                params->split = 't';
                break;

            case 'h':
            case '?':
            case -1:
            default:
                params->error = 1;
                break;
        }
    }

    return 1;
}

void
usage(char *prgName)
{

    printf("Usage: %s [-ipc] [-split]\n\n", prgName);
    printf(" -h,    --help                                        \n");
    printf("                  Help is printed.                  \n\n");
    printf(" -p,    --pipes                                       \n");
    printf("                  Use Pipes as IPC                  \n\n");
    printf(" -q,    --msg_queue                                   \n");
    printf("                  Use Msg Queue as IPC              \n\n");
    printf(" -m     --shared_memory                               \n");
    printf("                  Use Shared Memory as IPC          \n\n");
    printf(" -s     --sockets                                     \n");
    printf("                  Use Sockets as IPC                \n\n");
    printf(" -f     --fork                                        \n");
    printf("                  Use Fork to split process         \n\n");
    printf(" -t     --threads                                     \n");
    printf("                  Use Threads to split process      \n\n");

    return;
}
